package com.czk.workqueue.provider;

import com.czk.utils.RabbitmqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 工作队列生产者
 * @Author:ChenZhangKun
 * @Date: 2021/1/2 17:18
 */
public class Provider {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取链接
        Connection czkConnection = RabbitmqUtils.getCzkConnection();
        // 获取通道
        Channel channel = czkConnection.createChannel();
        // 声明工作队列
        channel.queueDeclare("work", true, false, false, null);
        // 生产消息
        for (int i = 0; i < 10; i++) {
            // MessageProperties.PERSISTENT_TEXT_PLAIN表示消息将持久化到磁盘
            channel.basicPublish("", "work", MessageProperties.PERSISTENT_TEXT_PLAIN, (i + "hello work queue").getBytes());
        }
        // 关闭通道
        RabbitmqUtils.closeConnection(channel, czkConnection);
    }
}
